package com.hdjk.view.callback;

import com.hdjk.view.entity.RiskTestEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GetRiskTestListCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject data = (JSONObject) msg.getData();
        if (data!=null) {
            Iterator<String> keys = data.keys();
            List<RiskTestEntity> list = new ArrayList<>();
            while (keys.hasNext()) {
                RiskTestEntity entity = new RiskTestEntity();
                String key = keys.next();
                if (key.equals("title")) {
                    continue;
                }
                if (!data.isNull(key)) {
                    entity.setNumber(key);
                    JSONObject content = data.getJSONObject(key);
                    if (!content.isNull("title")) {
                        entity.setTitle(content.getString("title"));
                    }
                    if (!content.isNull("option")) {
                        JSONObject option = content.getJSONObject("option");
                        List<RiskTestEntity.Option> options = new ArrayList<>();
                        Iterator<String> optKeys = option.keys();
                        while (optKeys.hasNext()) {
                            RiskTestEntity.Option opt = new RiskTestEntity().new Option();
                            String optKey = optKeys.next();
                            if (!option.isNull(optKey)) {
                                opt.setIndex(optKey);
                                JSONObject con = option.getJSONObject(optKey);
                                opt.setContent(con.getString("content"));
                            }
                            options.add(opt);
                        }
                        entity.setOption(options);
                    }
                }
                list.add(entity);
            }
            msg.setData(list);
        }
        return msg;
    }
}
